﻿namespace Blaze.Framework.Loaders
{
    using System.Collections.Generic;
    using UnityEngine;

    /// <summary>
    /// 用于同步资源加载器和游戏对象的生命周期。
    /// </summary>
    public class AssetLoaderLifecycle : MonoBehaviour
    {
        /// <summary>
        /// 增加一个资源加载器到列表中。
        /// </summary>
        /// <param name="loader">目标加载器</param>
        public void AddTarget(AssetLoaderBase loader)
        {
            mAssetLoaders.Add(loader);
        }

        public void RemoveTarget(AssetLoaderBase loader)
        {
            mAssetLoaders.Remove(loader);
        }

        private void OnDestroy()
        {
            foreach (var loader in mAssetLoaders)
                AssetLoaderPool.Put(loader);
        }

        private readonly HashSet<AssetLoaderBase> mAssetLoaders = new HashSet<AssetLoaderBase>();
    }
}